//
//  CMDBDatePicker.swift
//  ThreeStone
//
//  Created by 王磊 on 11/17/16.
//  Copyright © 2016 ThreeStone. All rights reserved.
//

import UIKit

protocol CMDBDatePickerDelegate: NSObjectProtocol {
    
    func confirmTriggered(datePicker: CMDBDatePicker)
}

class CMDBDatePicker: CMDBBaseView {
    
    private var cover: UIButton = UIButton.getInstance()
    
    private var bottomView: CMDBBaseView = CMDBBaseView.getInstance()
    
    private var datePicker: UIDatePicker = UIDatePicker()
    
    private var confirmBtn: UIButton = UIButton.getInstance()
    
    private var cancleBtn: UIButton = UIButton.getInstance()
    
    internal var dateString: String?
    
    internal var delegate: CMDBDatePickerDelegate?
    
    internal var id: String = ""
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(cover)
        
        addSubview(bottomView)
        
        bottomView.addSubview(datePicker)
        
        bottomView.addSubview(cancleBtn)
        
        bottomView.addSubview(confirmBtn)
        
        datePicker.frame = CGRectMake(0, 60 * CMDB_Rate, CMDB_Screen_Width, bottomView_height - 60 * CMDB_Rate)
        
        confirmBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(-20 * CMDB_Rate)
            make.width.equalTo(100 * CMDB_Rate)
            make.height.equalTo(50 * CMDB_Rate)
            make.top.equalTo(10 * CMDB_Rate)
        }
        cancleBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(20 * CMDB_Rate)
            make.width.equalTo(100 * CMDB_Rate)
            make.height.equalTo(50 * CMDB_Rate)
            make.top.equalTo(10 * CMDB_Rate)
        }
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        cover.backgroundColor = UIColor.blackColor()
        
        cover.alpha = 0
        
        bottomView.backgroundColor = UIColor.whiteColor()
        
        confirmBtn.setTitle("确认", forState: .Normal)
        
        confirmBtn.setTitle("确认", forState: .Highlighted)
        
        confirmBtn.setTitleColor(word87Color, forState: .Normal)
        
        confirmBtn.setTitleColor(word87Color, forState: .Highlighted)
        
        confirmBtn.titleLabel?.font = CMDB_17_FONT
        
        cancleBtn.setTitle("取消", forState: .Normal)
        
        cancleBtn.setTitle("取消", forState: .Highlighted)
        
        cancleBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
        
        cancleBtn.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        
        cancleBtn.titleLabel?.font = CMDB_17_FONT
        
        datePicker.datePickerMode = UIDatePickerMode.Date
        
        // 设置默认时间
        datePicker.setDate(NSDate(), animated: true)
        
        // 最小、最大时间选择设置
        //        $0.minimumDate = NSDate(timeInterval: -93312000, sinceDate: NSDate())
        datePicker.maximumDate = NSDate()
        
        // 时间分钟间隔设置
        datePicker.minuteInterval = 5
        
        // 将日期选择器区域设置为中文（默认显示英文）
        datePicker.locale = NSLocale(localeIdentifier: "zh_CN")
        
        cover.addTarget(self, action: #selector(coverTriggered), forControlEvents: .TouchUpInside)
        
        datePicker.addTarget(self, action: #selector(dateChanged(_:)), forControlEvents: .ValueChanged)
        
        cancleBtn.addTarget(self, action: #selector(coverTriggered), forControlEvents: .TouchUpInside)
        
        confirmBtn.addTarget(self, action: #selector(confirm), forControlEvents: .TouchUpInside)
    }
    
    internal var currentDate: NSDate?
    
    internal var datePickerMode: UIDatePickerMode? {
        willSet {
            if let _ = newValue {
                
                datePicker.datePickerMode = newValue!
                
            }
        }
        
    }
    
    internal var minDate: String? {
        willSet {
            
            let date = NSDate(timeIntervalSince1970: Double(newValue!)!)
            
            datePicker.minimumDate = date
            
            datePicker.setDate(date, animated: true)
        }
    }
    internal var maxDate: String? {
        
        willSet {
            let date = NSDate(timeIntervalSince1970: Double(newValue!)!)
            
            datePicker.maximumDate = date
            
        }
    }
}
extension CMDBDatePicker {
    
    override func update(data: AnyObject?) {
        let date = data as? NSString
        
        datePicker.setDate(NSDate(timeIntervalSince1970: date!.doubleValue), animated: true)
        
        self.currentDate = datePicker.date
    }
}
extension CMDBDatePicker {
    
    @objc private func coverTriggered() {
        
        dismiss(true)
    }
    
    @objc private func confirm() {
        
        dismiss(true)
        
        guard let delegate = delegate else {
            
            return
        }
        
        delegate.confirmTriggered(self)
    }
    @objc private func dateChanged(picker: UIDatePicker) {
        
        self.currentDate = picker.date
    }
}

private let bottomView_height: CGFloat = 500 * CMDB_Rate

extension CMDBDatePicker {
    
    internal func show(animated: Bool) {
        
        let window = UIApplication.sharedApplication().keyWindow
        
        frame = CMDB_Screen_Bounds
        
        window?.addSubview(self)
        
        cover.frame = bounds
        
        bottomView.addSubview(datePicker)
        
        bottomView.frame = CGRectMake(0, CMDB_Screen_Height, CMDB_Screen_Width, bottomView_height)
        
        self.currentDate = datePicker.date
        
        if animated {
            
            UIView.animateWithDuration(0.3, animations: {[weak self] in
                
                self!.cover.alpha = 0.5
                
                self!.bottomView.frame = CGRectMake(0, CMDB_Screen_Height - bottomView_height, CMDB_Screen_Width , bottomView_height)
                
                }, completion: { (flag) in
                    
                    
                    
            })
        }
    }
    internal func dismiss(animated: Bool) {
        
        if animated {
            UIView.animateWithDuration(0.3, animations: {[weak self] in
                self!.cover.alpha = 0
                
                self!.bottomView.frame = CGRectMake(0, CMDB_Screen_Height, CMDB_Screen_Width, bottomView_height)
                
                }, completion: { [weak self](flag) in
                    if flag {
                        
                        self!.removeFromSuperview()
                        
                    }
                })
        }
    }
}